<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * Author: IT宇宙人     
 * Date: 2015-09-09
 */
namespace Admin\Controller;
use Admin\Logic\GoodsLogic;
use Think\AjaxPage;
use Think\Page;

class CompanyController extends BaseController {




    /**
     *  商品列表
     */
    public function images_list(){

        $model = M('company_album');

        $count = $model->count();
        $Page  = new Page($count,10);
        /**  搜索条件下 分页赋值
        foreach($condition as $key=>$val) {
        $Page->parameter[$key]   =   urlencode($val);
        }
         */
        $show = $Page->show();
        $lists = $model->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('lists',$lists);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }


    /**
     * 添加修改
     */
    public function addEditImages()
    {
        $id=I('id');
        $Model=M('company_album');
        if(($_GET['is_ajax'] == 1) && IS_POST){
            if($id){
                //编辑
                $u_data['id']=$id;
                $u_data['name']=I('name');
                $Model->save($u_data);
                M('company_images')->where(array('a_id'=>$id))->delete();
                $images=I('images');
                $images_sort=I('images_sort');
                foreach($images AS $k=>$v){
                    if($v){
                        $i_data['images']=$v;
                        $i_data['a_id']=$id;
                        $i_data['sort']=$images_sort[$k];
                        M('company_images')->add($i_data);
                        unset($i_data);
                    }
                }
            }else{
                //添加
                $data['name']=I('name');
                $data['status']=1;
                $data['add_time']=time();
                $id=$Model->add($data);
                $images=I('images');
                $images_sort=I('images_sort');
                foreach($images AS $k=>$v){
                    if($v){
                        $i_data['images']=$v;
                        $i_data['a_id']=$id;
                        $i_data['sort']=$images_sort[$k];
                        M('company_images')->add($i_data);
                        unset($i_data);
                    }
                }
            }
            $return_arr = array(
                'status' => 1,
                'msg' => '操作成功',
                'data' => array('url' => U('Admin/Company/images_list')),
            );
            $this->ajaxReturn(json_encode($return_arr));
        }elseif($id){
            $where['id']=$id;
            $album=$Model->where($where)->find();
            $goodsImages = M("CompanyImages")->where('a_id =' . $id)->order("sort asc")->select();
            $this->assign('album', $album);  // 相册详情
            $this->assign('goodsImages', $goodsImages);  // 相册
        }


        $this->display('_images');
    }


    /**
     * 删除 delAlbum
     */
    public function delAlbum()
    {
        $id = $_GET['id'];


        // 删除
        //删除 图片

        $image_list = M("company_images")->where('a_id ='.$id)->select();  //删除商品云图片

        foreach($image_list AS $v){
            qiniu_del_images(end(explode('/',$v['image_url'])));//删除七牛云图片
        }

        M("company_album")->where('id ='.$id)->delete();  //商品表
        M("company_images")->where('a_id ='.$id)->delete();  //商品相册
        $this->success('操作成功',U('Company/images_list'));

//        $return_arr = array('status' => 1,'msg' => '操作成功','data'  =>'',);   //$return_arr = array('status' => -1,'msg' => '删除失败','data'  =>'',);
//        $this->ajaxReturn(json_encode($return_arr));
    }
    





}